//
//  GameOverPopUp.m
//  TowerDefence
//
//  Created by k3 on 6/27/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "GameOverPopUp.h"
#import "iCoreGUIController.h"

#define IMAGE_FLOWER @"Images/PlayScreen/hoa.jpeg"

@implementation GameOverPopUp
@synthesize gameLayer;

-(id)initGameOverPopUpWithWith:(GameLayer *)_gamLayer{
    self = [super init];
    gameLayer = _gamLayer;
    mainGUIController = [[iCoreGUIController alloc] initiCoreGUIControllerWithDirector:[CCDirector sharedDirector]];
    return self;
}

-(void)loadGameOverScreen {
    size = [[CCDirector sharedDirector] winSize];
    
    [gameLayer isTouchEnableForAllChild:NO];
    
    CCSprite *blankImg = [CCSprite spriteWithFile:BLANK_IMAGE];
    [blankImg setPosition:ccp(0, 0)];
    [blankImg setAnchorPoint:ccp(0, 0)];
    [blankImg setColor:ccc3(0, 0, 0)];
    [blankImg setOpacity:0];
    blankImg.textureRect = CGRectMake(0, 0, size.width, size.height);
    [blankImg runAction:[CCFadeTo actionWithDuration:0.15 opacity:120]];
    [gameLayer addChild:blankImg z:5];
    
    CCSprite *blankBrown = [CCSprite spriteWithFile:IMAGE_BLANK_BROWN];
    [blankBrown setPosition:ccp(size.width/2, size.height/2)];
    [blankBrown setAnchorPoint:ccp(0.5, 0.5)];
    [blankBrown runAction:[CCSequence actions:[CCScaleTo actionWithDuration:0.5 scale:1.3],[CCScaleTo actionWithDuration:0.5 scale:1], nil]];
    [blankImg addChild:blankBrown z:1];
    
    CCLabelTTF *gameOverLabel = [CCLabelTTF labelWithString:@"Game Over" fontName:FONT_COMIC_BOOK fontSize:22];
    gameOverLabel.color = ccc3(255, 255, 64);
    gameOverLabel.position = ccp(blankBrown.contentSize.width/2, blankBrown.contentSize.height-20);
    [blankBrown addChild:gameOverLabel z:10];
    
    CCSprite *loseScreen = [[CCSprite alloc] initWithFile:IMAGE_LOSE_SCREEN];
    [loseScreen setPosition:ccp(blankBrown.contentSize.width/2, blankBrown.contentSize.height/2+20)];
    [loseScreen setScale:0.9];
    [blankBrown addChild:loseScreen z:0];
    
    [mainGUIController loadGraphicFromPlistFileToFrameCache:PLIST_SHINE0_EFFECT];
    NSMutableArray *animArray = [self loadAnimationWithNumberOfFrames:21 withFrameName:@"shine0_effect"];
    CCAnimation *animation = [CCAnimation animationWithFrames:animArray delay:0.04f];
    CCSprite *temp = [CCSprite node];
    [temp runAction:[CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:animation]]];
    [temp setScale:1.3];
    [temp setPosition:ccp(80, 50)];
    [loseScreen addChild:temp z:0];
    
    CCSprite *temp1 = [CCSprite node];
    CCAnimation *animation1 = [CCAnimation animationWithFrames:animArray delay:0.08f];
    [temp1 runAction:[CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:animation1]]];
    [temp1 setPosition:ccp(165,90)];
    [temp1 setScale:1.3];
    [loseScreen addChild:temp1 z:0];
    
    CCSprite *btnNormal = [CCSprite spriteWithFile:IMAGE_MAIN_MENU_BUTTON_NORMAL];
    [btnNormal setPosition:ccp(blankBrown.contentSize.width-loseScreen.contentSize.width-20, blankBrown.contentSize.height-loseScreen.contentSize.height-35)];
    [blankBrown addChild:btnNormal z:1];
    
    CCLabelTTF *lblRestart = [CCLabelTTF labelWithString:@"Try Again" fontName:FONT_MARKER_FELT fontSize:22];
    [lblRestart setColor:ccc3(255, 255, 64)];
    CCMenuItemLabel *restartMenuItemLabel = [CCMenuItemLabel itemWithLabel:lblRestart target:self selector:@selector(BackToPlayScreen)];
    CCMenu *restartMenu = [CCMenu menuWithItems:restartMenuItemLabel, nil];
    [restartMenu setPosition:ccp(btnNormal.contentSize.width/2, btnNormal.contentSize.height/2)];
    [btnNormal addChild:restartMenu z:1];
    
    CCSprite *btnNormal01 = [CCSprite spriteWithFile:IMAGE_MAIN_MENU_BUTTON_NORMAL];
    [btnNormal01 setPosition:ccp(blankBrown.contentSize.width-btnNormal.contentSize.width-20, blankBrown.contentSize.height-loseScreen.contentSize.height-35)];
    [blankBrown addChild:btnNormal01 z:1];
    
    CCLabelTTF *lblQuit = [CCLabelTTF labelWithString:@"Map Menu" fontName:FONT_MARKER_FELT fontSize:24];
    [lblQuit setColor:ccc3(255, 255, 64)];
    CCMenuItemLabel *quitMenuItemLabel = [CCMenuItemLabel itemWithLabel:lblQuit target:self selector:@selector(BackToMapScreen)];
    CCMenu *quitMenu = [CCMenu menuWithItems:quitMenuItemLabel, nil];
    [quitMenu setPosition:ccp(btnNormal01.contentSize.width/2, btnNormal01.contentSize.height/2)];
    [btnNormal01 addChild:quitMenu z:2];
}

-(void)BackToPlayScreen {
    [mainGUIController PlayPlayScreenWithCurrentScreen:self.gameLayer mapDataXmlFile:[mainGUIController.listMap objectAtIndex:mainGUIController.currentMapRunning]];
}

-(void)BackToMapScreen {
    [mainGUIController PlayMapScreenWithCurrentScreen:self.gameLayer];
}

-(NSMutableArray*)loadAnimationWithNumberOfFrames:(int) numberOfFrames withFrameName:(NSString*)frame{
    NSMutableArray *anim=[[NSMutableArray alloc]init];
    for (int i=0;i<=numberOfFrames;i++)
    {
        NSString *frameName=[[NSString alloc]init];
        frameName=[NSString stringWithFormat:@"%@%d.png",frame,i];
        CCSpriteFrame *frame = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:frameName];
        [anim addObject:frame];
    }
    return anim;
}

@end
